package com.justdo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.justdo.R;
import com.justdo.annotation.ViewInject;
import com.justdo.imagecache.ImageFetcher;
import com.justdo.object.PublicTimelineObject;

public class PublicTimelineAdapter extends BaseModelAdapter<PublicTimelineObject>{
	private Context context;
	private LayoutInflater inflater;

	public PublicTimelineAdapter(Context context) {
		super();
		// TODO Auto-generated constructor stub
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View getCurrentView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		PublicTimelineObject obj = getItem(position);
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.publictimeline_layout, null);
			holder.head = (ImageView) convertView.findViewById(R.id.iv_head);
			holder.text = (TextView) convertView.findViewById(R.id.tv_text);
			holder.pic =(ImageView) convertView.findViewById(R.id.iv_pic);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		if(position % 2 == 1){
			convertView.setBackgroundResource(R.color.green_light);
		}else{
			convertView.setBackgroundResource(R.color.orange_light);
		}
		
		ImageFetcher if_head = new ImageFetcher(context, 480, 800);
		if_head.setImageFadeIn(false);
		if_head.setBeRoundCornered(true, 5);
		if_head.setLoadingImage(R.drawable.default_head);
		if_head.loadImage(obj.user.profile_image_url, holder.head);
		
		if(!TextUtils.isEmpty(obj.thumbnail_pic)){
			holder.pic.setVisibility(View.VISIBLE);
			ImageFetcher if_pic = new ImageFetcher(context, 480, 800);
//			if_pic.setImageViewMeasure((int)(DeviceTool.getDisplay((Activity)context)[0] * 0.3f), 0);
			if_pic.setImageFadeIn(false);
			if_pic.setLoadingImage(R.drawable.default_pic);
			if_pic.loadImage(obj.thumbnail_pic, holder.pic);
		}else{
			holder.pic.setVisibility(View.GONE);
		}
		
		holder.text.setText(obj.text);
		return convertView;
	}

	private class ViewHolder{
		ImageView head;
		TextView text;
		ImageView pic;
	}
}
